﻿using HamcoWcs.Entitys.DeviceMsgQueues;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.DeviceMsgQueues;
using HamcoWcs.Models.NodeInfos;
using System.Linq;

namespace HamcoWcs.Workers.SrmWorkers
{
    // 处理堆垛机任务相关逻辑
    public partial class PpkSrmWorker
    {
        private void DealTask(NodeInfoDto node)
        {
            var k = $"{node.NodeName}RECOVERYFLAG";
            var a = _config.TryGetConfigValue(k, out string converyFlag);
            var finder = _srmTaskFinderFactory.GetFinder();

            // 判断是否有复机信号，有的话不分配任务
            if (a)
            {
                if (converyFlag == "0")
                {
                    finder.PickSrm(node);
                }
                else
                {
                    var srm = node.GetEnabledSrm();

                    if (srm == null)
                    {
                        _logger.AddSystemErrorLog($"{node.NodeName}复机状态未清零时复机");
                        _config.SetConfigMsg(k, "0");
                    }
                    else
                    {
                        var subTasks = _wcsDbContext.SubTasks
                            .Where(x => x.TaskStatus == EnumSubTaskStatus.OnProgress.ToString())
                            .Where(x => x.PlcName == srm.SrmName)
                            .ToList();

                        if (subTasks.Any())
                        {
                            // 不做处理，等待执行完成
                        }
                        else
                        {
                            // 发送允许复机
                            var srmSendMsg = _srmMsgService.GetSrmRecorveryMsg();
                            var deviceMsgQueueDto = new DeviceMsgQueueDto(srm.AnotherSrm, srmSendMsg);
                            _wcsDbContext.DeviceMsgQueues.Add(_mapper.Map<DeviceMsgQueue>(deviceMsgQueueDto));
                            _wcsDbContext.SaveChanges();
                        }
                    }
                }
            }
            else
            {
                finder.PickSrm(node);
            }

            // todo 满足复机条件时发送允许复机


            foreach (var srm in node.Srms)
            {
                var subTask = finder.Find(srm);
                if (subTask == null) continue;

                DeviceMsgQueueDto deviceMsgQueueDto;



                //if (subTask.IsNeedSection && subTask.SubTaskType != EnumSubTaskType.SrmAbnormalInboundTask.ToString())
                if (subTask.IsNeedSection)
                {
                    if (subTask.SubTaskType == EnumSubTaskType.SrmAbnormalInboundTask.ToString())
                    {

                    }
                    var srmSendMsg = _srmMsgService.GetHandOffMsg(subTask);
                    deviceMsgQueueDto = new DeviceMsgQueueDto(srm, srmSendMsg);
                }
                else
                {
                    var srmSendMsg = _srmMsgService.GetSrmSendMsg(subTask);
                    deviceMsgQueueDto = new DeviceMsgQueueDto(srm, srmSendMsg);
                }

                if (srm.IsRepeated(deviceMsgQueueDto.MsgValue))
                {
                    continue;
                }

                // 获取是否存在相同的消息，队列中存在相同消息则不发送
                var t = _wcsDbContext.DeviceMsgQueues.Any(x => x.PlcName == srm.PlcName && x.MsgValue == deviceMsgQueueDto.MsgValue);
                if (t) continue;


                _wcsDbContext.DeviceMsgQueues.Add(_mapper.Map<DeviceMsgQueue>(deviceMsgQueueDto));
                _wcsDbContext.SaveChanges();
                _baseTaskService.UpdateSubTaskStatus(subTask.Id, EnumSubTaskStatus.OnProgress);
            }


        }

        //private void DealTask(SrmDto srm)
        //{
        //    if (!srm.IsCanDo()) return;
        //    SubTask taskEntity = _srmMsgService.FindSrmTask(srm);

        //    if (taskEntity == null) return;

        //    SrmSendMsg srmSendMsg = _srmMsgService.GetSrmSendMsg(taskEntity);
        //    var deviceMsgQueueDto = new DeviceMsgQueueDto(srm, srmSendMsg);
        //    //掉用移库接口
        //    //_wmsLocMoveTaskService.CreateLocMoveTask();
        //    if (srm.IsRepeated(deviceMsgQueueDto.MsgValue))
        //    {
        //        return;
        //    }
        //    _wcsDbContext.DeviceMsgQueues.Add(_mapper.Map<DeviceMsgQueue>(deviceMsgQueueDto));
        //    _wcsDbContext.SaveChanges();
        //    _baseTaskService.UpdateSubTaskStatus(taskEntity.Id, EnumSubTaskStatus.OnProgress);
        //}

    }
}
